#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"

class PolynomialSp1 {
private:
    NewtonInterpolation interpolation;
public:
    PolynomialSp1(const std::vector<double>& xVec, const std::vector<double>& yVec)
        : interpolation(xVec, yVec) {}

    std::vector<double> evaluate(double x) {
        return interpolation.interpolate(x);
    }
};

class PolynomialSp2 {
private:
    NewtonInterpolation interpolation;
public:
    PolynomialSp2(const std::vector<double>& xVec, const std::vector<double>& yVec)
        : interpolation(xVec, yVec) {}

    std::vector<double> evaluate(double x) {
        return interpolation.interpolate(x);
    }
};

int main() {
    std::vector<double> xVec = { 0, 6, 10, 13, 17, 20, 28 };
    std::vector<double> yVec1 = { 6.67, 17.3, 42.7, 37.3, 30.1, 29.3, 28.7 };
    std::vector<double> yVec2 = { 6.67, 16.1, 18.9, 15.0, 10.6, 9.44, 8.89 };

    PolynomialSp1 polynomialSp1(xVec, yVec1);
    PolynomialSp2 polynomialSp2(xVec, yVec2);

    std::ofstream file1("data_sp1.csv");
    if (!file1.is_open()) {
        std::cerr << "Failed to open file" << std::endl;
        return 1;
    }
    for (double x = 0; x <= 35; x += 0.01) {
        std::vector<double> y = polynomialSp1.evaluate(x);
        file1 << x << "," << y[0] << std::endl; // 假设返回的是单维度结果
    }
    file1.close();

    std::ofstream file2("data_sp2.csv");
    if (!file2.is_open()) {
        std::cerr << "Failed to open file" << std::endl;
        return 1;
    }
    for (double x = 0; x <= 35; x += 0.01) {
        std::vector<double> y = polynomialSp2.evaluate(x);
        file2 << x << "," << y[0] << std::endl; // 假设返回的是单维度结果
    }
    file2.close();
    return 0; 
}